import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React from "react";
import classes from "./ona.module.less";
import classNames from "classnames";
import { useMount, useReactive, useUpdateEffect } from "ahooks";
import useServices from "../../../hooks/useRequest";
import useCalculateAmount from "../../../hooks/useCalculateAmount";
import { accountAmount, getTokenAssets } from "../../../api/wallet";
import utilsFilter from "../../../utils/filters";
import { useDispatch, useSelector } from "react-redux";
import { useLocalStorageSetState } from "../../../hooks";
import { Power } from "../../../compoents/styled";
import { matchTheme } from "../../../utils";
import { CHAIN_MAP, isDevChain, TEST_CHAIN, MAIN_CHAIN } from "../../../config";
import Big from "big.js";
import { useTranslation } from "react-i18next";
import { LocalKeyMap } from "../../../local";
import ChainSwitch from "../../../compoents/chain-switch";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var Ona = /*#__PURE__*/ React.forwardRef(function(props, ref) {
    var getaccountAmount = function getaccountAmount() {
        services.getTokenAssets({
            pageNum: 1,
            pageSize: 1000,
            chainType: isDevChain ? TEST_CHAIN : MAIN_CHAIN
        }).then(function(res) {
            if (res.code == 200) {
                var _res_data, _res_data_accountAmount;
                var assets = res.data.list || [];
                res.data.accountAmount = coininfo.map(function(coin) {
                    var inAsset = assets.find(function(a) {
                        return "".concat(coin.chainType).concat(coin.contractAddress.toLocaleLowerCase()) === "".concat(a.chainType).concat(a.contract.toLocaleLowerCase());
                    });
                    return _object_spread_props(_object_spread({}, coin), {
                        coinId: coin.id,
                        uniqueId: "".concat(coin.chainType).concat(coin.contractAddress),
                        coinName: coin.name,
                        convertActualAmount: inAsset ? calculate([
                            {
                                contract: inAsset.contract,
                                balance: inAsset.balance,
                                chainType: inAsset.chainType
                            }
                        ])[0] : 0,
                        balance: (inAsset === null || inAsset === void 0 ? void 0 : inAsset.balance) || "0",
                        formatBalance: new Big((inAsset === null || inAsset === void 0 ? void 0 : inAsset.balance) || "0").div("1e".concat(coin.digits)).toString(),
                        digits: coin.digits
                    });
                });
                ((_res_data = res.data) === null || _res_data === void 0 ? void 0 : (_res_data_accountAmount = _res_data.accountAmount) === null || _res_data_accountAmount === void 0 ? void 0 : _res_data_accountAmount.sort(function(a, b) {
                    return b.convertActualAmount - a.convertActualAmount;
                })) || [];
                state.account = {
                    totalActualAmount: calculate(assets.map(function(token) {
                        return {
                            balance: token.balance,
                            contract: token.contract,
                            chainType: token.chainType
                        };
                    })).reduce(function(pre, next) {
                        return new Big(pre).plus(next).toString();
                    }, "0"),
                    accountAmount: res.data.accountAmount
                };
                dispatch.app.SET_ACCOUNT_INFO(state.account);
                if (!(localState === null || localState === void 0 ? void 0 : localState.chain)) {
                    var accountAmountAndChainType = state.account.accountAmount.map(function(token) {
                        return {
                            convertActualAmount: token.convertActualAmount,
                            chainType: token.chainType
                        };
                    });
                    var maxBalanceChain = getMaxBalanceChain(accountAmountAndChainType);
                    var _maxChinType = maxBalanceChain.maxChainType;
                    dispatch.app.SET_CHAIN(_maxChinType);
                    setLocalState({
                        chain: _maxChinType
                    });
                    state.chainType = CHAIN_MAP[_maxChinType];
                // state.chainType = _maxChinType;
                }
            }
        // this.isscrollcoinref = FLIPPED_ALIAS_KEYS
        }).catch(function(err) {
            console.log("err", err);
        });
    };
    var _utilsFilter_toFormatUSD, _utilsFilter_toFormatUSD1, _state_account_accountAmount_filter, _state_account_accountAmount;
    var _useLocalStorageSetState = _sliced_to_array(useLocalStorageSetState(), 2), localState = _useLocalStorageSetState[0], setLocalState = _useLocalStorageSetState[1];
    var _useSelector = useSelector(function(st) {
        return {
            theme: st.app.theme,
            coininfo: st.app.tokenCoins,
            chain: st.app.chain
        };
    }), theme = _useSelector.theme, coininfo = _useSelector.coininfo, chain = _useSelector.chain;
    var _useCalculateAmount = useCalculateAmount(), calculate = _useCalculateAmount.calculate, coinMap = _useCalculateAmount.coinMap, getMaxBalanceChain = _useCalculateAmount.getMaxBalanceChain;
    var t = useTranslation().t;
    var state = useReactive({
        // chain: localState?.chain || 'BSC',
        chainType: CHAIN_MAP[localState === null || localState === void 0 ? void 0 : localState.chain],
        billList: [],
        account: {
            totalActualAmount: "0",
            accountAmount: []
        }
    });
    var dispatch = useDispatch();
    var services = useServices({
        accountAmount: accountAmount,
        getTokenAssets: getTokenAssets
    });
    useMount(function() {
        if (!(coininfo === null || coininfo === void 0 ? void 0 : coininfo.length)) {
            dispatch.app.setCoinInfos().then(function() {
                getaccountAmount();
            });
        } else {
            getaccountAmount();
        }
    });
    useUpdateEffect(function() {
        getaccountAmount();
    }, [
        coininfo.length
    ]);
    // const changeChain = () => {
    //   if (chain === 'BSC') {
    //     dispatch.app.SET_CHAIN('ETHEREUM');
    //     setLocalState({
    //       chain: 'ETHEREUM',
    //     });
    //     state.chainType = ETHEREUM;
    //   } else if (chain == 'ETHEREUM') {
    //     dispatch.app.SET_CHAIN('CONFLUX');
    //     setLocalState({
    //       chain: 'CONFLUX',
    //     });
    //     state.chainType = CONFLUX;
    //   } else {
    //     dispatch.app.SET_CHAIN('BSC');
    //     setLocalState({
    //       chain: 'BSC',
    //     });
    //     state.chainType = BSC;
    //   }
    // };
    var goCoinDetail = function(params) {
        dispatch.app.PUSH({
            path: "/txn",
            query: {
                accountAmount: JSON.parse(JSON.stringify(state.account.accountAmount)),
                activeCoin: JSON.parse(JSON.stringify(_object_spread({}, params)))
            }
        });
    };
    var goSendToken = function() {
        dispatch.app.PUSH({
            path: "/sendToken",
            query: {
                accountAmount: JSON.parse(JSON.stringify(state.account.accountAmount))
            }
        });
    };
    var goReceive = function() {
        dispatch.app.PUSH({
            path: "/receive"
        });
    };
    return /*#__PURE__*/ _jsxs(_Fragment, {
        children: [
            /*#__PURE__*/ _jsx("div", _object_spread_props(_object_spread({
                className: classes.ona,
                ref: ref
            }, props), {
                children: /*#__PURE__*/ _jsxs("div", {
                    className: classes.ona_wrap,
                    children: [
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.info,
                            children: [
                                /*#__PURE__*/ _jsxs("div", {
                                    className: classes.total_txt,
                                    children: [
                                        t(LocalKeyMap["tokens.total"]),
                                        ":"
                                    ]
                                }),
                                /*#__PURE__*/ _jsxs("div", {
                                    className: "".concat(classes.balance, " ").concat(state.account.totalActualAmount == "0" && classes.noBalance),
                                    children: [
                                        /*#__PURE__*/ _jsx("span", {
                                            className: classes.unit,
                                            children: "US$"
                                        }),
                                        /*#__PURE__*/ _jsx("span", {
                                            className: classes.integer,
                                            children: (_utilsFilter_toFormatUSD = utilsFilter.toFormatUSD(state.account.totalActualAmount, 0)) === null || _utilsFilter_toFormatUSD === void 0 ? void 0 : _utilsFilter_toFormatUSD.split(".")[0]
                                        }),
                                        state.account.totalActualAmount != "0" && /*#__PURE__*/ _jsxs("span", {
                                            className: classes.decimals,
                                            children: [
                                                ".",
                                                ((_utilsFilter_toFormatUSD1 = utilsFilter.toFormatUSD(state.account.totalActualAmount, 0)) === null || _utilsFilter_toFormatUSD1 === void 0 ? void 0 : _utilsFilter_toFormatUSD1.split(".")[1]) || "00"
                                            ]
                                        })
                                    ]
                                }),
                                /*#__PURE__*/ _jsxs("div", {
                                    className: classes.options,
                                    children: [
                                        /*#__PURE__*/ _jsxs("div", {
                                            className: classNames(classes.btn),
                                            onClick: goSendToken,
                                            children: [
                                                /*#__PURE__*/ _jsx("img", {
                                                    className: classes.icon,
                                                    src: require("../../../assets/icons/send.svg")
                                                }),
                                                /*#__PURE__*/ _jsx("span", {
                                                    children: t(LocalKeyMap.send)
                                                })
                                            ]
                                        }),
                                        /*#__PURE__*/ _jsxs("div", {
                                            onClick: goReceive,
                                            className: classNames(classes.btn),
                                            children: [
                                                /*#__PURE__*/ _jsx("img", {
                                                    className: classes.icon,
                                                    src: require("../../../assets/icons/receive.svg")
                                                }),
                                                /*#__PURE__*/ _jsx("span", {
                                                    children: t(LocalKeyMap.receive)
                                                })
                                            ]
                                        })
                                    ]
                                })
                            ]
                        }),
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.currency,
                            children: [
                                /*#__PURE__*/ _jsx("div", {
                                    className: classes.head,
                                    children: /*#__PURE__*/ _jsx(ChainSwitch, {
                                        onChainChange: function(nc) {
                                            state.chainType = CHAIN_MAP[nc];
                                        }
                                    })
                                }),
                                /*#__PURE__*/ _jsx("div", {
                                    className: classes["srcoller_viewport"],
                                    children: /*#__PURE__*/ _jsx("div", {
                                        className: classes.scroller,
                                        children: /*#__PURE__*/ _jsx("div", {
                                            className: classes.scroll_wrap,
                                            children: /*#__PURE__*/ _jsx("div", {
                                                className: classes.list,
                                                children: (_state_account_accountAmount_filter = (_state_account_accountAmount = state.account.accountAmount) === null || _state_account_accountAmount === void 0 ? void 0 : _state_account_accountAmount.filter(function(i) {
                                                    var _state_chainType;
                                                    return (_state_chainType = state.chainType) === null || _state_chainType === void 0 ? void 0 : _state_chainType.includes(i.chainType);
                                                })) === null || _state_account_accountAmount_filter === void 0 ? void 0 : _state_account_accountAmount_filter.map(function(bill, index) {
                                                    return /*#__PURE__*/ _jsxs("div", {
                                                        onClick: function() {
                                                            return goCoinDetail(bill);
                                                        },
                                                        className: classes.list_item,
                                                        children: [
                                                            /*#__PURE__*/ _jsxs("div", {
                                                                className: classes.coin,
                                                                children: [
                                                                    /*#__PURE__*/ _jsx("img", {
                                                                        className: classes["coin_icon"],
                                                                        src: bill.imgLink
                                                                    }),
                                                                    /*#__PURE__*/ _jsx("div", {
                                                                        className: classes.name,
                                                                        children: bill.coinName
                                                                    }),
                                                                    /*#__PURE__*/ _jsxs("div", {
                                                                        className: classes.value,
                                                                        children: [
                                                                            /*#__PURE__*/ _jsx("div", {
                                                                                className: classes.num,
                                                                                children: utilsFilter.toFormatEther(bill.balance, bill.digits)
                                                                            }),
                                                                            bill.convertActualAmount != 0 && /*#__PURE__*/ _jsxs("div", {
                                                                                className: classes.usd,
                                                                                children: [
                                                                                    "US$",
                                                                                    utilsFilter.toFormatUSD("".concat(bill.convertActualAmount), 0)
                                                                                ]
                                                                            })
                                                                        ]
                                                                    })
                                                                ]
                                                            }),
                                                            /*#__PURE__*/ _jsx("div", {
                                                                className: classes.arrow,
                                                                children: /*#__PURE__*/ _jsx("img", {
                                                                    src: matchTheme(theme, {
                                                                        light: require("../../../assets/icons/light/rightOLArrow.svg"),
                                                                        dark: require("../../../assets/icons/dark/rightOLArrow.svg")
                                                                    })
                                                                })
                                                            })
                                                        ]
                                                    }, index);
                                                })
                                            })
                                        })
                                    })
                                })
                            ]
                        })
                    ]
                })
            })),
            /*#__PURE__*/ _jsx(Power, {
                children: t(LocalKeyMap.powerByOna)
            })
        ]
    });
});
export default Ona;

 //# sourceMappingURL=index.js.map